#include "main/utils.h"
using namespace std;

bool invalid = 0;
int str2Int(string str) {
  invalid = 0;
  if (str.size() == 0) {
    invalid = 1;
    return 0;
  }

  int positive = 1;
  if (str[0] == '-') {
    positive = -1;
    str = str.substr(1);
  } else if (str[0] == '+') {
    str = str.substr(1);
  }

  if (str.size() == 0) {
    invalid = 1;
    return 0;
  }

  long num = 0;
  for (int i = 0; i < str.size(); ++i) {
    if (str[i] < '0' || str[i] > '9') {
      invalid = 1;
      return 0;
    } else {
      num = num * 10 + (str[i] - '0') * positive;
      if (num > 0x7fffffff || num < (signed int)0x80000000) {
        invalid = 1;
        return 0;
      }
    }
  }
  return num;
}

int main() {
  vector<string> str_vec = {"", "123", "+123", "-123", "1a33", "+0", "-0", "+2147483647",
                      "-2147483647", "+2147483648", "-2147483648", "+2147483649",
                      "-2147483649", "+", "-"};
  for (int i = 0; i < str_vec.size(); ++i) {
    cout << str_vec[i] << " " << str2Int(str_vec[i]) << " invalid: " << invalid << endl;
  }

  return 0;
}
